Setting up your Python Environment

Posit::conf (2023)

Posit Academy

Getting up

XKCD: Python Environment

PyCon 2022:

Calvin Hendryx-Parker: Bootstrapping Your Local Python Environment Calvin Hendryx-Parker: Bootstrapping Your Local Python Environment

Why installing Python is confusing

  • There’s so many different ways of installing it
  • Python is popular
    • Operating systems come with python (for its own use)

Opinionated Python installation

Pyenv’s Virtual Environment Manager

Pros

Cons

  • Windows pyenv doesn’t support plugins
  • Your convenience might cause confusion for other people if they don’t use the same system

Posit Solutions Engineering Guide

Install pyenv

Which python

Real Python: Managing Multiple Python Versions With pyenv

Virtual environments

venv

  1. First make sure you are using the python version you want
  • e.g., pyenv local, pyenv shell
  1. Create the virtual environment with venv:
$ python -m venv venv # will create a venv folder in your working directory
$ python -m venv .venv # will create a .venv folder in your working directory
  1. Activate the venv:
$ source venv/bin/activate # on mac / linux
C:\> venv/Scripts\activate.bat # on windows, use Activate.ps1 for powershell
  1. Install your python packages
$ pip install pandas
$ pip install -r requirements.txt

Short pyenv cheatsheet

  • which python: will always point to pyenv
    • pyenv which python: give you the path to python in use
  • pyenv versions: See all installed versions and which one in use

  • pyenv install --list: list all available python to install
    • pyenv install --list | grep 3.11: list all python 3.11.x
  • pyenv install 3.11.5: install python 3.11.5

  • pyenv global <PYTHON VERSION>: sets the default python
  • pyenv local <PYTHON VERSION>: creates a .python-version file
  • pyenv shell <PYTHON VERSION>: uses <PYTHON VERSION> right now